yjx.controller('accountController', ['$rootScope', '$scope', '$fetch', 'modalService', '$timeout', '$filter',function ($rootScope, $scope, $fetch, modalService, $timeout,$filter) {
    var params={};
    var baseYear = 2017;
    var  now= new Date();
    var yearNow = now.getFullYear();
    var monthNow = now.getMonth();
    var dateNow = now.getDate();        
    var starDate = new Date(yearNow,monthNow);
    var lastData = dateNow;//用于寄存上次date长度
    var endlastData = dateNow;
    $scope.starTime = starDate; //模板绑定数据
    $scope.endTime = now;//
    var picker;
    var endpicker;
    var star = angular.element(document.getElementById('starPicker'));
    var end = angular.element(document.getElementById('endPicker'));

    var yearModel = [ { id: 2017, value: '2017年' }]
    function  pushYear(){
        var length = yearNow-baseYear;
        for(var i=0;i<length;i++){
            yearModel.push({id:i+1+baseYear,value:i+1+baseYear+'年'})
        }
    }
    pushYear();//年份model动态生成
    var yearIndex;
    for (var i = 0; i < yearModel.length; i++) {
        if (yearModel[i].id == yearNow) {
            $scope.selectedYear = yearModel[i];
            yearIndex = i;
        }
    }
    var monthModel = [
        { id: 1, value: '1月' },
        { id: 2, value: '2月' },
        { id: 3, value: '3月' },
        { id: 4, value: '4月' },
        { id: 5, value: '5月' },
        { id: 6, value: '6月' },
        { id: 7, value: '7月' },
        { id: 8, value: '8月' },
        { id: 9, value: '9月' },
        { id: 10, value: '10月' },
        { id: 11, value: '11月' },
        { id: 12, value: '12月' },
    ]
    var monthModelNow = monthModel.slice(0,monthNow+1);
    var dayModel_31 = [
        { id: 1, value: '1日' },
        { id: 2, value: '2日' },
        { id: 3, value: '3日' },
        { id: 4, value: '4日' },
        { id: 5, value: '5日' },
        { id: 6, value: '6日' },
        { id: 7, value: '7日' },
        { id: 8, value: '8日' },
        { id: 9, value: '9日' },
        { id: 10, value: '10日' },
        { id: 11, value: '11日' },
        { id: 12, value: '12日' },
        { id: 13, value: '13日' },
        { id: 14, value: '14日' },
        { id: 15, value: '15日' },
        { id: 16, value: '16日' },
        { id: 17, value: '17日' },
        { id: 18, value: '18日' },
        { id: 19, value: '19日' },
        { id: 20, value: '20日' },
        { id: 21, value: '21日' },
        { id: 22, value: '22日' },
        { id: 23, value: '23日' },
        { id: 24, value: '24日' },
        { id: 25, value: '25日' },
        { id: 26, value: '26日' },
        { id: 27, value: '27日' },
        { id: 28, value: '28日' },
        { id: 29, value: '29日' },
        { id: 30, value: '30日' },
        { id: 31, value: '31日' },

    ]
    var dayModel_30 = [
        { id: 1, value: '1日' },
        { id: 2, value: '2日' },
        { id: 3, value: '3日' },
        { id: 4, value: '4日' },
        { id: 5, value: '5日' },
        { id: 6, value: '6日' },
        { id: 7, value: '7日' },
        { id: 8, value: '8日' },
        { id: 9, value: '9日' },
        { id: 10, value: '10日' },
        { id: 11, value: '11日' },
        { id: 12, value: '12日' },
        { id: 13, value: '13日' },
        { id: 14, value: '14日' },
        { id: 15, value: '15日' },
        { id: 16, value: '16日' },
        { id: 17, value: '17日' },
        { id: 18, value: '18日' },
        { id: 19, value: '19日' },
        { id: 20, value: '20日' },
        { id: 21, value: '21日' },
        { id: 22, value: '22日' },
        { id: 23, value: '23日' },
        { id: 24, value: '24日' },
        { id: 25, value: '25日' },
        { id: 26, value: '26日' },
        { id: 27, value: '27日' },
        { id: 28, value: '28日' },
        { id: 29, value: '29日' },
        { id: 30, value: '30日' },
    ]
    var dayModel_29 = [
        { id: 1, value: '1日' },
        { id: 2, value: '2日' },
        { id: 3, value: '3日' },
        { id: 4, value: '4日' },
        { id: 5, value: '5日' },
        { id: 6, value: '6日' },
        { id: 7, value: '7日' },
        { id: 8, value: '8日' },
        { id: 9, value: '9日' },
        { id: 10, value: '10日' },
        { id: 11, value: '11日' },
        { id: 12, value: '12日' },
        { id: 13, value: '13日' },
        { id: 14, value: '14日' },
        { id: 15, value: '15日' },
        { id: 16, value: '16日' },
        { id: 17, value: '17日' },
        { id: 18, value: '18日' },
        { id: 19, value: '19日' },
        { id: 20, value: '20日' },
        { id: 21, value: '21日' },
        { id: 22, value: '22日' },
        { id: 23, value: '23日' },
        { id: 24, value: '24日' },
        { id: 25, value: '25日' },
        { id: 26, value: '26日' },
        { id: 27, value: '27日' },
        { id: 28, value: '28日' },
        { id: 29, value: '29日' },
    ]
    var dayModel_28 = [
        { id: 1, value: '1日' },
        { id: 2, value: '2日' },
        { id: 3, value: '3日' },
        { id: 4, value: '4日' },
        { id: 5, value: '5日' },
        { id: 6, value: '6日' },
        { id: 7, value: '7日' },
        { id: 8, value: '8日' },
        { id: 9, value: '9日' },
        { id: 10, value: '10日' },
        { id: 11, value: '11日' },
        { id: 12, value: '12日' },
        { id: 13, value: '13日' },
        { id: 14, value: '14日' },
        { id: 15, value: '15日' },
        { id: 16, value: '16日' },
        { id: 17, value: '17日' },
        { id: 18, value: '18日' },
        { id: 19, value: '19日' },
        { id: 20, value: '20日' },
        { id: 21, value: '21日' },
        { id: 22, value: '22日' },
        { id: 23, value: '23日' },
        { id: 24, value: '24日' },
        { id: 25, value: '25日' },
        { id: 26, value: '26日' },
        { id: 27, value: '27日' },
        { id: 28, value: '28日' },
    ]
    var dayModelNow = dayModel_31.slice(0,dateNow);
    $scope.refillData = dayModelNow;
    $scope.endrefillData = dayModelNow;
    $scope.yearChange = yearNow;
    $scope.endyearChange = yearNow;
    /**根据月份与年份充填date数据 */
    function resetDayModel(month, year, isStar) {
        if(month==monthNow+1 && year==yearNow){
            if(isStar){
                $scope.refillData = dayModelNow;
            }else{
                $scope.endrefillData = dayModelNow;
            }
        }
        else if (month % 2 == 0) {
            if (month == 2) {
                if (isStar) {
                    $scope.refillData = year % 4 == 0 ? dayModel_29 : dayModel_28;
                } else {
                    $scope.endrefillData = year % 4 == 0 ? dayModel_29 : dayModel_28;
                }

            } else if (month == 10 || month == 12) {
                isStar ? $scope.refillData = dayModel_31 : $scope.endrefillData = dayModel_31;
            } else {
                isStar ? $scope.refillData = dayModel_30 : $scope.endrefillData = dayModel_30;
            }
        } else {
            if (month != 11) {
                isStar ? $scope.refillData = dayModel_31 : $scope.endrefillData = dayModel_31;
            } else {
                isStar ? $scope.refillData = dayModel_30 : $scope.endrefillData = dayModel_30;
            }
        }
    }

    /**选择器上初始化数据  */
    $scope.dateData = [yearModel, monthModelNow, $scope.refillData];
    $scope.dateIndex = [yearIndex, monthNow, 0];
    $scope.enddateData = [yearModel, monthModelNow, $scope.endrefillData];
    $scope.enddateIndex = [yearIndex, monthNow, dateNow-1];
    /**选择器上数据改变事件 */
    function dateChange(index, selectedIndex) {
        if (index == 0) {
            /**年份change */
            $scope.yearChange = yearModel[selectedIndex].id;
            if($scope.yearChange==yearNow){
                picker.refillColumn(1,monthModelNow);
            }else {
                picker.refillColumn(1,monthModel);
            }
            if ($scope.yearChange % 4 == 0) {
                if ($scope.monthChange == 2) {
                    $scope.refillData = dayModel_29;
                }
            }
            if (lastData != $scope.refillData.length) {
                picker.refillColumn(2, $scope.refillData);
                lastData = $scope.refillData.length;
            }
        } else if (index == 1) {
            /**月份change */
            $scope.monthChange = monthModel[selectedIndex].id;
            resetDayModel($scope.monthChange, $scope.yearChange, true);
            if (lastData != $scope.refillData.length) {
                $timeout(function () {
                    picker.refillColumn(2, $scope.refillData)
                })

                lastData = $scope.refillData.length;
            }
        }
    }
    function enddateChange(index, selectedIndex) {
        if (index == 0) {
            /**年份change */
            $scope.endyearChange = yearModel[selectedIndex].id;
            if($scope.enddateChange==yearNow){
                endpicker.refillColumn(1,monthModelNow);
            }else {
                endpicker.refillColumn(1,monthModel);
            }
            if ($scope.endyearChange % 4 == 0) {
                if ($scope.endmonthChange == 2) {
                    $scope.endrefillData = dayModel_29;
                }
            }
            if (endlastData != $scope.endrefillData.length) {
                endpicker.refillColumn(2, $scope.endrefillData);
                endlastData = $scope.endrefillData.length;
            }
        } else if (index == 1) {
            /**月份change */
            $scope.endmonthChange = monthModel[selectedIndex].id;
            resetDayModel($scope.endmonthChange, $scope.endyearChange, false);
            if (endlastData != $scope.endrefillData.length) {
                endpicker.refillColumn(2, $scope.endrefillData);
                endlastData = $scope.endrefillData.length;
            }
        }
    }
    /**选择器上确认完成事件 */
    function dateSelect(value, index) {
        $timeout(function(){
            $scope.starTime = new Date(yearModel[index[0]].id,monthModel[index[1]].id-1,dayModel_31[index[2]].id);
            
        })
     
    }
    function enddateSelect(value, index) {
        $timeout(function(){
            $scope.endTime = new Date(yearModel[index[0]].id,monthModel[index[1]].id-1,dayModel_31[index[2]].id);
        })
    }

    /**创建选择器 */
    function buildPickerStar() {
        picker = new Picker({
            data: $scope.dateData,
            selectedIndex: $scope.dateIndex,
            title: ''
        });
        picker.on('picker.select', function (selectedVal, selectedIndex) {
            dateSelect(selectedVal, selectedIndex);
        })

        picker.on('picker.change', function (selectedVal, selectedIndex) {
            dateChange(selectedVal, selectedIndex);
        });
        star.bind('click', function () {
            picker.show()
        })
    }
    buildPickerStar();

    function buildPickerEnd() {
        endpicker = new Picker({
            data: $scope.enddateData,
            selectedIndex: $scope.enddateIndex,
            title: ''
        });
        endpicker.on('picker.select', function (selectedVal, selectedIndex) {
            enddateSelect(selectedVal, selectedIndex);
        })
        endpicker.on('picker.change', function (selectedVal, selectedIndex) {
            enddateChange(selectedVal, selectedIndex);
        });
        end.bind('click', function () {
            endpicker.show()
        })
    }
    buildPickerEnd();

    /**按范围选账单 */
    $scope.resetAccount = function (){
        if($scope.endTime < $scope.starTime){
            var middleTime = $scope.endTime;
            $scope.endTime = $scope.starTime;
            $scope.starTime = middleTime;
        }
        $scope.tansEndTime = $filter('date')($scope.endTime, 'yyyy-MM-dd');
        $scope.tansStarTime = $filter('date')($scope.starTime, 'yyyy-MM-dd')
        getAccount();
    }
    /**获取收支列表 */
    function getAccount(){
        if($scope.tansEndTime && $scope.tansStarTime){
            params.startTime = $scope.tansStarTime;
            params.endTime = $scope.tansEndTime;
        }
        $fetch.fetch(
            '/incomeAndExpenditure/me',
            {
                params:params
            }
        ).then(function(data){
            $timeout(function(){
                $scope.income =data.total?data.total.brokerage:0;
                $scope.expense = data.total?data.total.expenditure:0;
                $scope.accountList = data.list;
            })
        }).catch(function(e){
            modalService.mainMsgError(e.msg);
        })
    }
    getAccount();
}])